<?php

namespace app\model\v1\recovery;


use constants\ProductConstants;
use crmeb\traits\ModelTrait;
use think\Exception;
use think\exception\ValidateException;
use think\facade\Db;
use think\Model;

class RecoveryFeedBack extends Model
{
    use ModelTrait;

    protected $updateTime = false;
    protected $createTime = false;

    protected $hidden = [
    //    'create_time','update_time','delete_time'
    ];

    protected $table = 'eb_recovery_feedback';


    public function item()
    {
        return $this->hasMany(RecoveryFeedBackItem::class, 'f_id', 'id');
    }

    const QUESTION_TOP_TYPE = 1;   //1 分类问题

    const QUESTION_BOOTTOM_TYPE = 2;   //2 热门问题


    /**
     * 个人中心 - 常见问题 - 获取问题分类的类型
     * @param $params
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public static function getFeedBackData($params = [])
    {
        $data["list"] = self::field("id,title_icon,list_item,status")
            ->where("type", self::QUESTION_TOP_TYPE)
            ->select()
            ->toArray();

        $data["top_img"] = Db::name("config")->where("key", 'tbj_question_top_img')->find() ?? [];

        $data["data"] = self::field("id,title_icon,list_item,status")
            ->with(['item'=> function($query) {
                $query->where('status',1)->field('*');
            }])
            ->where("type", self::QUESTION_BOOTTOM_TYPE)
            ->select()
            ->toArray();

        return $data;
    }





    /**
     * 个人中心 - 获取问题反馈分类
     * @param $params
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getQuestionCate($params = [])
    {
        //根据用户传参的type类型获取问题反馈分类的数据
        $data = Db::name("recovery_question_cate")
            ->where("type", $params["type"])
            ->where("status", 1)
            ->select()->toArray();
        return $data;
    }


    /**
     * 个人中心 - 问题反馈提交
     * @param $param
     * @return int|string
     */
    public static function createQuestion($param = [])
    {
        //收集用户反馈问题提交的信息
        $add_data = [
            "uid" => $param["user_id"],  //用户id
            "cate_id" => $param["cate_id"],  //分类id
            "desc" => $param["desc"],  //描述
            "img" => $param["img"] ?? "",  //图片
            "create_time" => time(),  //创建时间
        ];
        $res = Db::name("recovery_question")->insert($add_data);
        return $res;
    }


    /**
     * 问题分类管理列表
     * @param $param
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getFeedbackCateList($param = [])
    {
        $order = ["id"=>"desc"];

        $data['data'] = self::field ("*,FROM_UNIXTIME(create_time) as create_time,
            case when status=1 then '上架'
                when status =2 then '下架'
                end as status_text
            ")
            ->where(function ($query) use ($param){
                $query->whereNull("delete_time");
                $query->where("type", 1);
                if(!empty($param["word"])) $query->where("list_item",'like', '%'.$param["word"].'%');
                if(!empty($param['time'])){    //根据创建日期搜索
                    $wheretime = explode (',',$param['time']);
                    $query->where('create_time', 'between', [strtotime($wheretime[0]),strtotime($wheretime[1])]);
                }
            })
            ->order($order)
            ->page($param['page'], $param['limit'])
            ->select()->toArray();

        $data['count'] = self::field ("*")
            ->where(function ($query) use ($param){
                $query->whereNull("delete_time");
                $query->where("type", 1);
                if(!empty($param["word"])) $query->where("list_item",'like', '%'.$param["word"].'%');
                if(!empty($param['time'])){    //根据创建日期搜索
                    $wheretime = explode (',',$param['time']);
                    $query->where('create_time', 'between', [strtotime($wheretime[0]),strtotime($wheretime[1])]);
                }
            })
            ->count();

        return $data;
    }



    public static function updateFeedback($param = [])
    {
        $update["update_time"] = time();  //更新时间
        if(!empty($param["list_item"]))     $update["list_item"] = $param["list_item"];  //分类名称
        if(!empty($param["type"]))          $update["type"] = $param["type"]; //类型
        if(!empty($param["title_icon"]))    $update["title_icon"] = $param["title_icon"]; //分类图片

        return self::where("id", $param["id"])->update($update);
    }



    public static function addFeedback($param = [])
    {
        $add = [
            "uid"           => $param["admin_id"],  //创建人id
            "list_item"     => $param["list_item"] ?? '', //分类名称
            "type"          => $param["type"] ?? '1',  //类型
            "title_icon"    => $param["title_icon"] ?? '',  //分类图片
            "create_time"   => time(),
        ];
        return self::insert($add);
    }


    public static function getQuestionCategoryList($param = [])
    {
        return RecoveryFeedBack::whereNull("delete_time")->whereIn("type", [1,2])->field("id,list_item as type_name")->select()->toArray();
    }



    public static function enableFeedback($param = [])
    {
        $status = self::where("id", $param['id'])->value("status");
        $msg = '';
        if($status == 1){   //1启用 2禁用
            $enable = 2;
            $msg = "禁用成功";
        }else{
            $enable = 1;
            $msg = "启用成功";
        }
        self::where("id", $param['id'])->update(['status' => $enable]);
        return $msg;
    }



    public static function getFeedbackItemType($params = [])
    {
        $id = self::where("list_item", "交易须知")->value("id") ?? 0;
        return RecoveryFeedBackItem::whereNull("delete_time")->where("f_id", $id)->where("status", 1)->where("content","like", '%出价%')->find();
    }
}